// 分类模块
// 存储的分类数据
import { topCategory } from '@/api/constants'
import { findAllCategory } from '@/api/category'
import { ActionsFunctionContext, CategoryListItem, CategoryStateType } from '@/types/vuexTypes'
import { ResultAxiosResponse } from '@/types/axiosTypes'

export default {
    namespace: true,
    state() {
        return {
            // 分类信息集合,默认为自己定义的九大类
            list: topCategory.map(item => ({ id:0, name: item })),
            showMenuId: ''
        }
    },
    // 加载数据成功后需要修改list所以需要mutations函数
    mutations: {
      setList (state: CategoryStateType, headCategory: CategoryListItem[]) {
        state.list = headCategory
      },
      show (state: CategoryStateType, menuId: string) {
        state.showMenuId = menuId
      },
      hide (state: CategoryStateType) {
        state.showMenuId = ''
      },
    },
    // 需要向后台加载数据，所以需要actions函数获取数据
    actions: {
      async getList ({ commit }: ActionsFunctionContext) {
        const { result } = await findAllCategory() as ResultAxiosResponse
        // 获取数据成功，提交mutations进行数据修改
        commit('setList', result)
      }
    }
}